import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @Title: messageWall
 * @Author 郑雨杭
 * @Package PACKAGE_NAME
 * @Date 2024/7/26 22:31
 * @description:
 */

class Mes{
    private String from;
    private String to;
    private String message;

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "Mes{" +
                "from='" + from + '\'' +
                ", to='" + to + '\'' +
                ", message='" + message + '\'' +
                '}';
    }

    public Mes() {
    }

    public Mes(String from, String to, String message) {
        this.from = from;
        this.to = to;
        this.message = message;
    }
}

@WebServlet("/message")
public class messageWall extends HttpServlet {

    private ObjectMapper mapper = new ObjectMapper();
    private DataSource dataSource= new MysqlDataSource();
    //private ArrayList<Mes> list = new ArrayList<>();


    @Override
    public void init() throws ServletException {
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/mesage_wall?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("dopost");
        Mes mes = mapper.readValue(req.getInputStream(),Mes.class);
        System.out.println(mes);
        try {
            save(mes);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.setStatus(200);
        resp.getWriter().write("ok");
        /*resp.setContentType("application/json");
        resp.getWriter().write("{ok:true}");*/
    }

    private void save(Mes mes) throws SQLException {
        Connection connection = dataSource.getConnection();
        String sql = "insert INTO mess VALUES(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,mes.getFrom());
        preparedStatement.setString(2,mes.getTo());
        preparedStatement.setString(3,mes.getMessage());
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setStatus(200);
        resp.setContentType("application/json;charset=utf8");
        ArrayList<Mes> list = null;
        try {
            list = load();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //Iterator<Mes> iterator = list.iterator();
        resp.getWriter().write(mapper.writeValueAsString(list));
    }

    private ArrayList<Mes> load() throws SQLException {
        Connection connection = dataSource.getConnection();
        String sql = "select * from mess";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        ArrayList<Mes> list = new ArrayList<>();
        while (resultSet.next()){
            list.add(new Mes(resultSet.getString("from"),
                    resultSet.getString("to"),
                    resultSet.getString("mes")));
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        return list;
    }
}
